<?php

class MallAttrController extends Controller
{

    /*
    * 获取商品属性列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = MallSpecs::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = MallSpecs::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $type = Util::getParamNotEmpty("type");
            $unit_name = Util::getParam("unit_name",null);


            //添加
            $obj = new MallSpecs();
            Util::initObjData($obj);


            $obj->name = $name;
            $obj->type = $type;
            $obj->unit_name = $unit_name;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");


            //验证存在
            $obj = MallAttr::model()->findByPK($obj_id);

                Util::checkExistAndNotDelete($obj);

                //修改信息

                $obj->name = $name;

                //保存
                Util::saveObjData($obj);



                //返回数据
                Util::responseMsg(MsgConst::RETURN_SUCCESS);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //验证是否存在子菜单
            $searchsql = Util::initSearchSql();
            $searchsql = Util::getSerarchStrAdd($searchsql,"attr_id","$obj_id");
            $childcount = MallMaterial::model()->count($searchsql);

            if($childcount >= 1){
                throw new Exception("该属性下有属性内容存在，请先删除其下的属性内容");
            }



            //删除
            $obj = MallAttr::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 获取商品属性内容列表分页
    *
    */
    public function actionGetContentListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $attr_id = Util::getParamNotEmpty("attr_id");

            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql,"attr_id",$attr_id);

            //获取count
            $totalcount = MallAttrContent::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = MallAttrContent::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 添加
    *
    */
    public function actionAddContent()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $attr_id = Util::getParamNotEmpty("attr_id");
            $content = Util::getParamNotEmpty("content");


            //添加
            $obj = new MallAttrContent();
            Util::initObjData($obj);


            $obj->attr_id = $attr_id;
            $obj->content = $content;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 修改
    *
    */
    public function actionUpdateContent()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $content = Util::getParamNotEmpty("content");


            //验证存在
            $obj = MallAttrContent::model()->findByPK($obj_id);

                Util::checkExistAndNotDelete($obj);

                //修改信息

                $obj->content = $content;

                //保存
                Util::saveObjData($obj);



                //返回数据
                Util::responseMsg(MsgConst::RETURN_SUCCESS);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 删除
    */
    public function actionDeleteContent()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = MallAttrContent::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


}